package tech.aistar.corejava.day10.homework;

/**
 * Created by 李雨昂 on 2019/7/26.
 */
public class Demo01 {
    public static void main(String[] args) {

        //StringBuilder builder = new StringBuilder("Welcome");

        /*StringBuilder builder1 = new StringBuilder("fileviewgoodrunbuildfilehelp");
        StringBuilder builder2 = new StringBuilder("fileviewgoodrunbuildfilehelp");
        StringBuilder builder3 = new StringBuilder("fileviewgoodrunbuildfilehelp");
        System.out.println(builder1.reverse());
        System.out.println(builder2.subSequence(8,20));
        System.out.println(builder3.substring(builder3.length()-5));*/
        String str = "Welcome";

        System.out.println(encryption(str));
        System.out.println(decrypt(encryption(str)));


    }

    public static char[] encryption(String str){
        //new 一个StringBuilder类型
        StringBuilder stringBuilder= new StringBuilder(str);
        //取到第一个字符拼接到后面
        stringBuilder.append(stringBuilder.charAt(0));
        //删除第一个字符
        stringBuilder.deleteCharAt(0);
        //new 一个char型数组arr
        char[] arr = new char[stringBuilder.length()];
        int pos = 0;//定义一个计数器
        for (int i = 0; i < stringBuilder.length(); i++) {
            //将每个字符+3后强转成char类型存到数组arr中
            arr[pos++] = (char) (stringBuilder.charAt(i) + 3);
        }
        return arr;
    }
    public static String decrypt(char[] arr){
        //new 一个char型数组arr
        char[] array = new char[arr.length];
        int pos = 0;//定义一个计数器
        for (int i = 0; i <arr.length; i++) {
            //将每个字符-3后强转成char类型存到数组arr中
            array[pos++] = (char) (arr[i] - 3);
        }
        //字符数组转成字符串类型
        String str = new String(array);
        //字符串转成StringBuilder类型
        StringBuilder stringBuilder= new StringBuilder(str);
        //取最后一个字符添加到第0号位
        stringBuilder.insert(0,stringBuilder.charAt(arr.length - 1));
        //删除最后一位字符
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        //StringBuilder转成字符串类型
        String bt =stringBuilder.toString();
        return bt;
    }


}
